package com.innovation.chengzhangriji.ui.widget;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class JoinViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickJoinListener {
        void onClickJoin(JoinViewHolder holder);
    }

    private OnClickJoinListener mOnClickJoinListener;

    private CuteTextView mTextName;

    private CuteBabyAvatarView mImageHead;

    private ImageView mImageSelect;

    private DisplayImageOptions options; //配置图片加载及显示选项

    public JoinViewHolder(View itemView) {
        super(itemView);
        // ImageView imageNote = (ImageView) itemView.findViewById(R.id.image_note);
        mTextName = (CuteTextView) itemView.findViewById(R.id.text_name);
        // mImageHead = (RoundedImageView) itemView.findViewById(R.id.image_head);
        mImageHead = (CuteBabyAvatarView) itemView.findViewById(R.id.image_head);
        mImageSelect = (ImageView) itemView.findViewById(R.id.image_select);
        mImageHead.setOnClickListener(this);
        setOptions();
    }

    public void setOnClickJoinListener(OnClickJoinListener onClickJoinListener) {
        mOnClickJoinListener = onClickJoinListener;
    }

    public void setHead(String headUrl, int sex) {
        // ImageLoader.getInstance().displayImage(headUrl, mImageHead, options);
        mImageHead.setAvatar(headUrl);
        mImageHead.setSex(sex);
    }

    public void setText(CharSequence text) {
        mTextName.setText(text);
    }

    public void setSelectVisibility(boolean visibility) {
        if (visibility) {
            mImageSelect.setVisibility(View.VISIBLE);
        } else {
            mImageSelect.setVisibility(View.INVISIBLE);
        }
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_unknown)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_unknown)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_unknown)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.image_head) {
            if (mOnClickJoinListener != null) {
                mOnClickJoinListener.onClickJoin(this);
            }
        }
    }
}
